// ==UserScript==
// @name         wc change
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://supermind.10jqka.com.cn/notebook/user/565286428/notebooks/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=10jqka.com.cn
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
    "use strict";
    let currentType = "";
    let isKp = false;
    let host = "http://192.168.56.138:8081";
    function task() {
        return new Promise(function (resolve, reject) {
            //做一些异步操作

            let count = 0;
            let task = setInterval(function () {
                count += 1;
                let table = document.querySelector("table");
                if (table) {
                    clearInterval(task);
                    resolve(table.outerHTML);
                }
                if (count > 5) {
                    clearInterval(task);
                    reject("获取失败");
                }
            }, 500);
        });
    }

    function get_type() {
        let wc_type = null;
        let code = document.querySelectorAll(".CodeMirror-code")[0];
        let is_dzj = code.innerText.includes(
            "dde大单净量大于0.2，dde大单净量从大到小，涨幅大于1%，排除科创板，排除创业板，排除北交所，排除st"
        );
        let is_wc1 = code.innerText.includes(
            "流通市值小于300亿，量比大于1.3，涨幅大于2，股价大于均线价，分时均价线向上，均线多头排列，股价大于9日均线，9日均线向上，昨日换手率大于4%，获利比率大于80%，屏蔽创业板，屏蔽科创板"
        );
        let is_wc2 = code.innerText.includes(
            "量比大于1.3，涨幅大于2，股价大于均线价，分时均价线向上，股价大于9日均线，9日均线向上，昨日换手率大于3%，获利比率大于80%，屏蔽创业板，屏蔽BJ， 屏蔽科创板"
        );
        let cjdb = code.innerText.includes('大于昨日的成交量/14400，热门股200，市值小于300亿，主板，排除st');
        if (is_dzj) {
            wc_type = "dzj";
        }
        if (is_wc1) {
            wc_type = "wc1";
        }
        if (is_wc2) {
            wc_type = "wc2";
        }
        if (cjdb) {
            wc_type = "cjdb"
        }
        return wc_type;
    }

    function get_html() {
        let timeout = setTimeout(() => {
            try {
                let code = document.querySelectorAll(".CodeMirror-code")[0];
                code.click();
            } catch (err) {
                console.log(err, "点击失败");
                return get_html()
            }

            let wc_type = get_type();
            let err_msg = document.querySelector("#notification_kernel").querySelector("span").title
            if (err_msg == "点击重连") {
                console.log("服务器中断");
                let data = {
                    type: "service_err",
                    msg: "服务器中断",
                };
                data = JSON.stringify(data)
                GM_xmlhttpRequest({
                    method: "post",
                    url: `${host}/wc_change/service_err`,
                    headers: {
                        "Content-Type":"application/json;charset=utf-8",
                        "Authorization": "1b4e51052da1a6b1211942a34c9cc1ad"
                    },
                    timeout: 15000,
                    data: data,
                    onload: function (response) {
                        var data = response.responseText;
                        data = JSON.parse(data);
                        data = data["response"];
                        console.log(data);
                        currentType = data.current_type;
                        isKp = data.is_kp;
                        return get_html();
                    },
                    onerror: function (response) {
                        console.log("请求失败");
                        return get_html();
                    },
                    ontimeout: function (response) {
                        console.log("请求超时");
                        return get_html();
                    },
                });
            } else if (!isKp) {
                console.log("未开盘，监听中");
                console.log(`当前类型：${wc_type}`)
                let data = {
                    type: "heartbeat",
                    msg: "heartbeat",
                };
                data = JSON.stringify(data)
                GM_xmlhttpRequest({
                    method: "post",
                    url: `${host}/wc_change/heartbeat`,
                    headers: {
                        "Content-Type":"application/json;charset=utf-8",
                        "Authorization": "1b4e51052da1a6b1211942a34c9cc1ad"
                    },
                    timeout: 15000,
                    data: data,
                    onload: function (response) {
                        var data = response.responseText;
                        data = JSON.parse(data);
                        data = data["response"];
                        console.log(data);
                        currentType = data.current_type;
                        isKp = data.is_kp;
                        return get_html();
                    },
                    onerror: function (response) {
                        console.log("请求失败");
                        return get_html();
                    },
                    ontimeout: function (response) {
                        console.log("请求超时");
                        return get_html();
                    },
                });
            }
            if (isKp) {
                console.log("继续操作", isKp);
                document
                    .querySelector("#run_int")
                    .querySelector("button")
                    .click();
                task()
                    .then((res) => {
                        console.log(new Date());
                        let data = {
                            type: wc_type,
                            msg: res,
                        };
                        data = JSON.stringify(data)
                        GM_xmlhttpRequest({
                            method: "post",
                            url: `${host}/wc_change/${wc_type}`,
                            headers: {
                                "Content-Type":"application/json;charset=utf-8",
                                "Authorization": "1b4e51052da1a6b1211942a34c9cc1ad"
                            },
                            timeout: 15000,
                            data: data,
                            onload: function (response) {
                                var data = response.responseText;
                                data = JSON.parse(data);
                                data = data["response"];
                                console.log(data);
                                currentType = data.current_type;
                                isKp = data.is_kp;
                                return get_html();
                            },
                            onerror: function (response) {
                                console.log("请求失败");
                                return get_html();
                            },
                            ontimeout: function (response) {
                                console.log("请求超时");
                                return get_html();
                            },
                        });
                        console.log("递归执行", timeout);
                    })
                    .catch((err) => {
                        console.log(err);
                        return get_html();
                    });
            }
        }, 1000);
    }
    // 初始调用
    get_html()
})();
